﻿// Groupon.NET C# Library
// Copyright (c) 2011 James Paul Duncan (JPaulDuncan)
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// http://www.jpaulduncan.com

namespace Groupon.Net
{
    internal sealed class DealsRequest : RequestBase
    {
        public DealsRequest()
            : base()
        {
            this.GeoCode = new GeoCode();
            this.GoogleAnalytics = new GoogleAnalytics();
        }

        /// <summary>
        /// Used for locating deals near a specific lat / lng
        /// </summary>
        public GeoCode GeoCode { get; set; }

        /// <summary>
        /// Distance (in miles) from the provided lat / lng within which to locate deals
        /// </summary>
        public int Radius { get; set; }

        /// <summary>
        /// The id of the division for which to get deals
        /// </summary>
        public string DivisionId { get; set; }

        /// <summary>
        /// When not logged in, this can be used to load targeted deal content based on client device token
        /// </summary>
        public string DeviceToken { get; set; }

        /// <summary>
        /// When not logged in, a subscription center id can be passed in this parameter to load targeted deals for that email address. A logged-in user or email_address parameter will take precedence over this value.
        /// </summary>
        public string SubscriberId { get; set; }

        /// <summary>
        /// If an affiliate id is passed in the affiliate parameter, the /deals response will contain only deals for that affiliate
        /// </summary>
        public string AffiliateId { get; set; }

        /// <summary>
        /// When not logged in, this can be used to load targeted deal content for a user.
        /// </summary>
        public string EmailAddress { get; set; }

        /// <summary>
        /// For divisions that have sub-areas, specify an area permalink to retrieve those deals.
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// Determines which values should be returned
        /// </summary>
        public ShowTypes Show { get; set; }

        /// <summary>
        /// Optional parameters for google utm tracking
        /// </summary>
        public GoogleAnalytics GoogleAnalytics { get; set; }

        public override string RESTCall
        {
            get
            {
                base.ResetParameters();
                base.AddParameter("lat", this.GeoCode.Latitude);
                base.AddParameter("lng", this.GeoCode.Longitude);
                base.AddParameter("radius", this.Radius.ToString());
                base.AddParameter("division_id", this.DivisionId);
                base.AddParameter("area", this.Area);
                base.AddParameter("email_address", this.EmailAddress);
                base.AddParameter("device_token", this.DeviceToken);
                base.AddParameter("subscriber_id", this.SubscriberId);
                base.AddParameter("show", this.Show == ShowTypes.Default ? "default" : "all");
                base.AddParameter("affiliate_id", this.AffiliateId);

                string parms = base.ParamsToQueryString();

                if (this.GoogleAnalytics != null) { parms += "&" + this.GoogleAnalytics.ToURLParams(); }

                return string.Format(Client.APIUri.ToString() + "deals.xml?{0}", parms);

            }
        }
    }
}